package com.yunkeji.admin.controller.moments;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.core.validate.AddGroup;
import com.yunkeji.common.core.validate.EditGroup;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.common.utils.poi.ExcelUtil;
import com.yunkeji.dao.vo.MomentsVo;
import com.yunkeji.system.domain.bo.CheckBo;
import com.yunkeji.system.domain.bo.MomentsBo;
import com.yunkeji.system.service.IMomentsService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 动态
 *
 * @author zhang
 * @date 2024-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/moments")
public class MomentsController extends BaseController {

    private final IMomentsService iMomentsService;

    /** 审核通过 */
    @SaCheckPermission("system:moments:pass")
    @Log(title = "-审核通过", businessType = BusinessType.INSERT)
    @PostMapping("/pass")
    public R<Void> pass(@RequestBody CheckBo bo) {
        iMomentsService.pass(bo);
        return R.ok();
    }

    /** 审核拒绝 */
    @SaCheckPermission("system:moments:refuse")
    @Log(title = "-审核拒绝", businessType = BusinessType.INSERT)
    @PostMapping("/refuse")
    public R<Void> refuse(@RequestBody CheckBo bo) {
        iMomentsService.refuse(bo);
        return R.ok();
    }
    /** 查询动态列表 */
    @SaCheckPermission("system:moments:list")
    @GetMapping("/list")
    public TableDataInfo<MomentsVo> list(MomentsBo bo, PageQuery pageQuery) {

        return iMomentsService.queryPageList(bo, pageQuery);
    }

    /** 导出动态列表 */
    @SaCheckPermission("system:moments:export")
    @Log(title = "动态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MomentsBo bo, HttpServletResponse response) {
        List<MomentsVo> list = iMomentsService.queryList(bo);
        ExcelUtil.exportExcel(list, "动态", MomentsVo.class, response);
    }

    /**
     * 获取动态详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:moments:query")
    @GetMapping("/{id}")
    public R<MomentsVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iMomentsService.queryById(id));
    }

    /** 新增动态 */
    @SaCheckPermission("system:moments:add")
    @Log(title = "动态", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MomentsBo bo) {
        return toAjax(iMomentsService.insertByBo(bo));
    }

    /** 修改动态 */
    @SaCheckPermission("system:moments:edit")
    @Log(title = "动态", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MomentsBo bo) {
        return toAjax(iMomentsService.updateByBo(bo));
    }

    /**
     * 删除动态
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:moments:remove")
    @Log(title = "动态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iMomentsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
